//
//  IMTCANR0MessageCoder.h
//  INMOTION
//
//  Created by gzc on 2018/10/16.
//  Copyright © 2018年 Inmotion. All rights reserved.
//
// r0e的组包与解包封装

#import <Foundation/Foundation.h>

#define CMD_READ  0x1       /// 读
#define CMD_WRITE 0x3       /// 写
#define CMD_BlackBox 0x5    /// 黑匣子
#define CMD_IAP 0x7         /// IAP升级

#define DEV_MAIN_CTRL 0x0A  /// 将报文发给主控板
#define DEV_MAIN_BLE  0x0B  /// 将报文发给蓝牙板
#define DEV_APP       0x0D  /// 将报文发送给APP

@interface IMTCANR0MessageCoder : NSObject

// 解析数据并组包用结构
typedef struct
{
    int    RxPackIndex;                    /// 接收到第几个数据了
    int    RxPackEnd;                      /// 一共要接收多少个数据
    unsigned short RxPackCheck;            /// 报文校验值
    unsigned char RxPackBuffer[256];
    bool RxPackHand;                       /// 报头监测
    bool RxPackHandMaybe;
    unsigned char KeyIndex;
    unsigned int RcvOkPack;                /// 校验正确的报文数量
    unsigned int RcvErrPack;               /// 失败的报文数量
}S_Codec;


/**
 解包数据
 
 @param data 要解包的数据
 */
+ (void)unpackWithData:(NSData *)data resultBlock:(void (^)(NSData *data ,u_int8_t ID))resultBlock;

/**
 IAP解包数据
 
 data：数据段
 cmdID：命令编码
 otherID：推展功能码
 dev：发给谁
 @param data 要解包的数据
 */
+ (void)IAPUnpackWithData:(NSData *)data resultBlock:(void (^)(NSData *data , unsigned char cmdID , unsigned char otherID , unsigned char dev))resultBlock;

/**
 向R0发送指令
 
 @param messageID 要读取对象的索引
 @param length 要读取的长度
 @param isRead YES:读命令 NO：写命令
 @return 返回打包后的Data
 */
+ (NSData *)dataWithMessageID:(u_int32_t)messageID length:(unsigned char)length isRead:(BOOL)isRead;


+ (NSData *)dataWithMessageID:(u_int32_t)messageID withData:(unsigned char *)data length:(unsigned char)length;

//+ (NSData *)dataWithIAPMessageID:(u_int32_t)messageID withData:(unsigned char *)data length:(unsigned char)length;
+ (NSData *)dataWithIAPCMD:(unsigned char)cmdHeader MessageID:(u_int32_t)messageID withData:(unsigned char *)data length:(unsigned char)length;
+ (NSData *)dataToDeviceWithIAPCMD:(unsigned char)cmdHeader MessageID:(u_int32_t)messageID withData:(unsigned char *)data length:(unsigned char)length toDevice:(BOOL)isble;

/// 发送数据 --- 带有发送设备的
+ (NSData *)dataWithIAPCMD:(unsigned char)cmdHeader toDevice:(BOOL )isBle MessageID:(u_int32_t)messageID withData:(unsigned char *)data length:(unsigned char)length;

@end

